package com.sskj.b70.p.fragment;

import com.lzy.okgo.OkGo;
import com.lzy.okrx2.adapter.FlowableBody;
import com.sskj.b70.m.HttpConfig;
import com.sskj.b70.m.StockCoinBean;
import com.sskj.b70.m.bean.HttpData;
import com.sskj.b70.m.http.JsonConverter;
import com.sskj.b70.m.http.WebSocketObserver;
import com.sskj.b70.p.base.BasePresenter;
import com.sskj.b70.utils.DisposUtil;
import com.sskj.b70.v.fragment.MarketListFragment;

import java.util.List;

import io.reactivex.Flowable;
import io.reactivex.disposables.Disposable;

/**
 * Created by Lee on 2018/2/28 0028.
 */
public class MarketFragmentPst extends BasePresenter<MarketListFragment> {


    private WebSocketObserver btcWebSocket;
    private WebSocketObserver timeWebSocket;
    String text = "{\"type\":\"vb_okex\"}";
    private Disposable timeDisposable;
    private Disposable btcDisposable;

    public Flowable<List<StockCoinBean>> getData(String code) {
        return OkGo.<HttpData<List<StockCoinBean>>>post(HttpConfig.BASE_URL + HttpConfig.PRODUCT_LIST)
                .params("type", 4)
                .params("code", code)
                .converter(new JsonConverter<HttpData<List<StockCoinBean>>>() {
                })
                .adapt(new FlowableBody<>())
                .map(HttpData::getData);
    }


//    public void initWebSocket() {
//        if (btcWebSocket == null || timeWebSocket == null) {
//            btcWebSocket = new WebSocketObserver(HttpConfig.BTC_WS) {
//                @Override
//                public void onOpen(WebSocket webSocket, Response response) {
//                    webSocket.send(text);
//                }
//            };
//            btcDisposable = btcWebSocket.subscribe(message -> mView.update(message));
//            timeWebSocket = new WebSocketObserver(HttpConfig.BTC_WS) {
//
//                @Override
//                public void onOpen(WebSocket webSocket, Response response) {
//                    webSocket.send(text);
//                }
//            };
//            timeDisposable = timeWebSocket.subscribe(message -> mView.updateTime(message));
//        } else {
//            btcWebSocket.send(text);
//            timeWebSocket.send(text);
//        }
//
//    }

    public void closeWebSocket() {
        DisposUtil.close(btcDisposable);
        DisposUtil.close(timeDisposable);
    }

    @Override
    public void detachView() {
        super.detachView();
        closeWebSocket();
    }

}
